package com.example.mongodb.mongodb.controller.rabbitmq.zhifu;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitmqZhifuConfig {

    //创建正常队列，与死信队列
    //创建正常交换器，死信交换器
    //绑定正常交换器与队列，死信交换器与队列


    @Bean
    public Queue zhengChangQueue(){
        Map<String,Object> map = new HashMap<String,Object>();
        //指定消息队列中的消息过期时间
        map.put("x-message-ttl",20000);
//        map.put("x-expires",10000);
        //指定过期消息通过死信交换器发送到死信队列，死信交换器的名称DLX
        map.put("x-dead-letter-exchange","siXinExchange");
        //指定死信交换器的路由key
        map.put("x-dead-letter-routing-key","zhifu.siXin");
        return new Queue("zhengChangQueue",true,false,false,map);
    }

    @Bean
    public Queue siXinQueue(){

        return new Queue("siXinQueue",false,false,false);
    }




    @Bean
    public Exchange zhengChangExchange(){
        return new DirectExchange("zhengChangExchange",true,false);
    }

    @Bean
    public Exchange siXinExchange(){

        return new DirectExchange("siXinExchange",true,false);
    }


    //绑定正常队列与正常交换器
    @Bean
    public Binding zhengChangBinding(){
        return BindingBuilder.bind(zhengChangQueue()).to(zhengChangExchange()).with("zhifu").noargs();
    }

    @Bean
    public Binding siXinBinding(){
        return BindingBuilder.bind(siXinQueue()).to(siXinExchange()).with("zhifu.siXin").noargs();
    }



    @Bean
    @Autowired
    public RabbitAdmin amqpAdmin1(ConnectionFactory connectionFactory){
        return  new RabbitAdmin(connectionFactory);
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory1(ConnectionFactory connectionFactory){
        SimpleRabbitListenerContainerFactory simpleRabbitListenerContainerFactory = new SimpleRabbitListenerContainerFactory();
        simpleRabbitListenerContainerFactory.setConnectionFactory(connectionFactory);
        //手动确认消息
        simpleRabbitListenerContainerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        //设置并发线程数
        simpleRabbitListenerContainerFactory.setConcurrentConsumers(10);
        //设置最大并发线程数
        simpleRabbitListenerContainerFactory.setMaxConcurrentConsumers(20);
        return simpleRabbitListenerContainerFactory;
    }


}
